
import Coordinate from  '../Coordinate.js';
import Board from  '../Board.js'

export default class Utils{

    constructor(){

    }

     getReBW(bw) {
        if (bw === 2) return 1;
        if (bw === 1) return 2;
      
        return 1;
      }

    // 将+0101-0202的字符串转化成Coordinate集合
     toCoordinateList(str) {
        let cs = []
        if (!str || str === "") return cs
      
        const max = Math.floor(str.length / 5)
        let ns = ""
        for (let i = 0; i < max; i++) {
          ns = str.substring(i * 5, (i + 1) * 5) // 5位为1手
          let bw=ns[0]
          // console.info('bw:',bw);
          if (ns.length === 5 && (bw === '+' || bw === '-')) {
            const x = parseInt(ns.substring(1, 3))
            const y = parseInt(ns.substring(3, 5))
            let color=0;
            if (bw === '+') {
              color = 1
            } else if (bw === '-') {
              color = 2
            } else {
              color = 3
            }

            // console.info('color:',color);
            const c = new Coordinate(y,x,19,color)

            // console.info('c:',c);
      
            cs.push(c)
          }
        }
        return cs
      } 


      showYzSteps(board,yzSteps, {refreshUI = true } = {}) {
        board.cleanGrid()
        if (!yzSteps || yzSteps === '') return;
        
        const cs = this.toCoordinateList(yzSteps);

        
        for(let i=0;i<cs.length;i++){

          let c=cs[i];

          board.setNextColor(c.bw);
          board.put(c.bw,c.x, c.y);

        }
        
        // 如果需要刷新UI
        // if (refreshUI) {
        //   board.postEvent();
        // }
        return board
      }

}